﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using DTO;
using AutoMapper;

namespace DataAccess
{
    public static class MatchDAO
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static MatchDAO()
        {
            XmlConfigurator.Configure();
        }

        public static int CountMatchList()
        {
            QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
            int count = (from m in context.MATCH_LIST
                         select m).Count();
            return count;
        }

        public static List<MatchDTO> GetMatchList()
        {
            try
            {
                List<MatchDTO> lstMatch = new List<MatchDTO>();

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = from Match in context.MATCH_LIST
                            select Match;
                foreach (var item in query)
                {
                    item.HOME_TEAMReference.Load();
                    item.AWAY_TEAMReference.Load();
                    item.ROUNDReference.Load();

                    MatchDTO dtoMatch = new MatchDTO();
                    dtoMatch = Mapper.Map<MATCH, MatchDTO>(item);
                    lstMatch.Add(dtoMatch);
                }
                return lstMatch;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }

        public static bool AddNewMatch(MatchDTO dtoMatch)
        {

            try
            {
                MATCH obj = new MATCH();
                //Mapping                
                obj.ATTENDANCE = dtoMatch.ATTENDANCE;
                obj.AWAY_SCORES = dtoMatch.AWAY_SCORES;
                obj.HOME_SCORES = dtoMatch.HOME_SCORES;
                obj.IS_PLAYED = dtoMatch.IS_PLAYED;
                obj.MATCH_DATETIME = dtoMatch.MATCH_DATETIME;               
                
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                //Tìm AwayTeam
                obj.AWAY_TEAM = context.TEAM_LIST.First(at => at.TEAM_ID == dtoMatch.AWAY_TEAM.TEAM_ID);
                //Tìm HomeTeam
                obj.HOME_TEAM = context.TEAM_LIST.First(at => at.TEAM_ID == dtoMatch.HOME_TEAM.TEAM_ID);
                //Tìm vòng đấu [round]
                obj.ROUND = context.ROUND_LIST.First(r => r.ROUND_ID == dtoMatch.ROUND.ROUND_ID);

                context.AddToMATCH_LIST(obj);
                context.SaveChanges(true);

                dtoMatch.MATCH_ID = obj.MATCH_ID;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool UpdateMatch(MatchDTO dtoMatch)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                MATCH obj = context.MATCH_LIST.First(m => m.MATCH_ID == dtoMatch.MATCH_ID);
                obj.ATTENDANCE = dtoMatch.ATTENDANCE;
                obj.AWAY_SCORES = dtoMatch.AWAY_SCORES;
                obj.HOME_SCORES = dtoMatch.HOME_SCORES;
                obj.MATCH_DATETIME = dtoMatch.MATCH_DATETIME;
                obj.IS_PLAYED = dtoMatch.IS_PLAYED;
                //Update Home Team
                obj.HOME_TEAMReference.Load();
                if (dtoMatch.HOME_TEAM.TEAM_ID != obj.HOME_TEAM.TEAM_ID)
                {
                    obj.HOME_TEAM = context.TEAM_LIST.First(at => at.TEAM_ID == dtoMatch.HOME_TEAM.TEAM_ID);
                }

                //Update Away Team
                obj.AWAY_TEAMReference.Load();
                if (dtoMatch.AWAY_TEAM.TEAM_ID != obj.AWAY_TEAM.TEAM_ID)
                {
                    obj.AWAY_TEAM = context.TEAM_LIST.First(at => at.TEAM_ID == dtoMatch.AWAY_TEAM.TEAM_ID);
                }
                //Update Round
                obj.ROUNDReference.Load();
                if (dtoMatch.ROUND.ROUND_ID != obj.ROUND.ROUND_ID)
                {
                    obj.ROUND = context.ROUND_LIST.First(r => r.ROUND_ID == dtoMatch.ROUND.ROUND_ID);
                }
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAMatch(MatchDTO dtoMatch)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                MATCH obj = context.MATCH_LIST.First(m => m.MATCH_ID == dtoMatch.MATCH_ID);
                context.DeleteObject(obj);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAllMatch()
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = (from match in context.MATCH_LIST
                             select match);
                foreach (var item in query)
                {
                    context.DeleteObject(item);
                }
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }
    }
}
